// index.js

const app = getApp() //app.globalData
Page({
  data: {
    city:'武汉',
    changeTime:'2024/1/3 10:00',
    speed: null,
    quality: '良',
    primaryPollutant: 'PM2.5',
    airMsgList:[
      {
        id:'pm25',
        name:'PM2.5',
        val:44,
        max:500
      },
      {
        id:'pm10',
        name:'PM10',
        val:62,
        max:600
      },
      {
        id:'so2',
        name:'SO₂',
        val:10,
        max:800
      },
      {
        id:'no2',
        name:'NO₂',
        val:32,
        max:940
      },
      {
        id:'o3',
        name:'O₃',
        val:65,
        max:1200
      },
      {
        id:'co',
        name:'CO',
        val:0.8,
        max:150
      },
    ],
    tempreture:'暂无数据',
    wind:'暂无数据',
    humidity:'暂无数据',
    timeIndex:'0',
    airType:['天气','AQI' ,'PM2.5','PM10','NO2','SO2','O3','CO'],
    typeIndex:0,
    lnglat:[114.305392, 30.593098],
    option:null,
  },
  // 获取城市空气质量数据
  getAirMsg(lnglat) {
    app.globalData.http.get('/ai-sensing-back-service/api/home/airquality',{
      lnglat: lnglat
    }).then(res=>{
      let airData = res.data.air
      let time
      let weatherData = res.data.weather
      let list = this.data.airMsgList

      if ( airData.timePoint.length == 14) {
        let year =  airData.timePoint.substr(0, 4);
        let month = parseInt( airData.timePoint.substr(4, 2));
        let day = parseInt( airData.timePoint.substr(6, 2));
        let hour = parseInt( airData.timePoint.substr(8, 2));
        let min =  airData.timePoint.substr(10, 2);
        time = `${year}/${month}/${day} ${hour}:${min}`;
      }
      this.setData({
        city:airData.area,
        speed:airData.aqi,
        changeTime:time,
      })
      list.map(e=>{
        for(let i in airData) {
          if(i == e.id) {
            e.val = airData[i]
          }
        }
        return e
      })
      this.setData({
        airMsgList:list
      })
    })

  },
  // 获取预测数据
  getForcast(lnglat) {
    app.globalData.http.get('/ai-sensing-back-service/api/home/forecast',{
      lnglat:lnglat,
      type:this.data.timeIndex == '0' ? '24小时' : '15天'
    }).then(res=>{
      this.drawgraphic(res.data.items)
    })
  },
  // 切换预测时间
  changeTimeIndex(e) {
    let index = e.currentTarget.dataset.item
    this.setData({
      timeIndex:index
    })
    this.getForcast(this.data.lnglat)
  },
  // 预报图标显示
  drawgraphic(data) {
    let xd=[],yd=[],z=[],weather=[];
    for(let i=0;i < data.length;i++){
      data[i].ptime = data[i].ptime.split('')
      data[i].ptime.splice(0,5)
      data[i].ptime = data[i].ptime.join('')
      xd.push(data[i].ptime);
      yd.push(data[i].maxTemperature);
      z.push(data[i].minTemperature);
      // weather.push(data[i].weather);
      weather.push(this.getweathericon(data[i].weather));
    }
    let is15Day = this.data.timeIndex !== '0'
    let option = {
      xAxis: [{
        type: 'category',
        data: xd,
        show:true,
        boundaryGap: false,
        axisLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        axisLabel: {
          show: true,
          interval: 0,
          margin: '25',
          formatter: function (val, i) {
            return '{'+i+'|}\n{'+ (i == 0 ? 'First'+'|现在}' : 'other'+'|'+val+'}');
          },
          rich: {

          }
        }
      }],
      dataZoom: {
        start: 0,
        end:30,
        type: "inside"
      },
      yAxis: {
        type: 'value',
        show:false,
        stack: 'Total',
      },
      grid:{
        left:'5%',
        top:is15Day?30:10,
        right:'5%',
        bottom:'35%',
        // bottom:is15Day?25:5,
        // width:'100%',
        // containLabel: true
      },
      series: [
        {
          name:'max',
          data: [],
          type: 'line',
          label: {
            show: is15Day,
            fontSize: 13,
            formatter:(val)=>{
              return val.value + '°'
            }
          },
          smooth: true,
          lineStyle: {
            color: "RGBA(129, 193, 254, 1)", //线条颜色
          },
          areaStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                  offset: 0, color: 'rgba(76, 151, 255, .3)' // 0% 处的颜色
              }, {
                  offset: 1, color: 'rgba(76, 151, 255,0.1)' // 100% 处的颜色
              }],
              global: false // 缺省为 false
            }
          },
        },
        {
          name:'min',
          show:is15Day,
          data: [],
          type: 'line',
          label: {
            show: true,
            offset: [0, 30],
              fontSize: 13,
            formatter:(val)=>{
              return val.value + '°'
            }
          },
          smooth: true,
          lineStyle: {
            color: "RGBA(129, 193, 254, 1)", //线条颜色
          },
          areaStyle: {
            color: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1,
              colorStops: [{
                  offset: 0, color: 'rgba(76, 151, 255, .1)' // 0% 处的颜色
              }, {
                  offset: 1, color: 'rgba(76, 151, 255, .1)' // 100% 处的颜色
              }],
              global: false // 缺省为 false
            }
          },
        }
      ]
    };	

    let obj = {
      First: {
        height: 20,
        align: 'center',
        color: 'rgba(255, 174, 3, 1)'
      },
      other: {
        height: 20,
        align: 'center',
      },
    }
    weather.map((e,i)=>{
      obj[i] = {
        height: 20,
        align: 'center',
        backgroundColor: {
          image: weather[i]
        }
      }

    })
    option.xAxis[0].axisLabel.rich = obj
    yd.map((e,index) =>{
      if(index == 0){
        let obj = {
          value:e,
          symbolSize:8,//拐点大小
          symbol: 'circle',//拐点样式                            
          itemStyle:{ 
            normal:{
              color: "rgba(255, 174, 3, 1)", //拐点颜色
              borderColor:'#fff',
              borderWidth:3
            },
          },
        }
        option.series[0].data.push(obj)
      }else{
        let obj = {
          value:e,
          symbolSize:8,
          symbol: 'circle',//拐点样式                            
          itemStyle:{ 
            normal:{
              color: "rgba(76, 151, 255, 1)", //拐点颜色
              borderColor:'#fff',
              borderWidth:3
            },
          },
        }
        option.series[0].data.push(obj)
      }
    })
    z.map((e,index) =>{
      if(index == 0){
        let obj = {
          value:e,
          symbolSize:8,//拐点大小
          symbol: 'circle',//拐点样式                            
          itemStyle:{ 
            normal:{
              color: "rgba(255, 174, 3, 1)", //拐点颜色
              borderColor:'#fff',
              borderWidth:3
            },
          },
        }
        option.series[1].data.push(obj)
      }else{
        let obj = {
          value:e,
          symbolSize:8,
          symbol: 'circle',//拐点样式                            
          itemStyle:{ 
            normal:{
              color: "rgba(76, 151, 255, 1)", //拐点颜色
              borderColor:'#fff',
              borderWidth:3
            },
          },
        }
        option.series[1].data.push(obj)
      }
    })
    this.setData({
      option:option
    }) 
  },

  // 天气图标
  getweathericon(weather){
    switch(weather)
    {
      case "小雨到大雨":
      case "中雨到大雨":
      case "阴到大雨":
      case "大雨":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/heavy_rain.png";
        break;
      case "小雨到中雨":
      case "中雨":
      case "阴到中雨":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/moderate_rain.png";
        break;
      case "小雨转阴":
      case "风转阴":
      case "晴转阴":
      case "小雪转阴":
      case "多云转阴":
      case "阴":
      case "中雨转阴":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/overcast.png";
        break;
      case "暴风雨":
      case "中雨到暴风雨":
      case "大雨到暴风雨":
      case "暴雨到暴风雨":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/storm02.png";
        break;
      case "风":
      case "小雪转风":
      case "晴转风":
      case "阴转风":
      case "多云转风":
      case "小雨转风":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/wind.png";
        break;
      case "小雨转雪":
      case "小雪":
      case "晴转雪":
      case "多云转雪":
      case "阴到中雪":
      case "风转雪":
      case "阴转雪":
      case "阴到小雪":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/light_snow.png";
        break;
      case "霾":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/smog.png";
        break;
      case "小雨到暴雨":
      case "大雨到暴雨":
      case "中雨到暴雨": 
      case "暴雨":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/rainstorm.png";
        break;
      case "小雨":
      case "阴到小雨":
      case "晴转雨":
      case "小雪转雨":
      case "风转雨":
      case "阴转雨":
      case "多云转雨":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/light_rain.png";
        break;
      case "中雪":
      case "小雪到中雪":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/moterate_snow.png";
        break;
      case "风转多云":
      case "晴转多云":
      case "阴转多云":
      case "小雪转多云":
      case "小雨转多云":
      case "多云":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/cloud.png";
        break;
      case "晴":
      case "多云转晴":
      case "小雨转晴":
      case "风转晴":
      case "霾转晴":
      case "小雪转晴":
      case "阴转晴":
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/sun.png";
        break;
      default:
        return "https://7072-prod-0g6d813n9889e2e6-1322465086.tcb.qcloud.la/login/moon.png";
    }
  },
  
  onLoad(options) {
    let lnglat = [parseFloat(decodeURIComponent(options.lng)),parseFloat(decodeURIComponent(options.lat))]
    if(lnglat[0]) {
      this.setData({
        lnglat:lnglat
      })
      app.globalData.lnglat = lnglat
      this.getAirMsg(lnglat)
    }else{
      this.getAirMsg([114.305392,30.593098])
    }
    this.getForcast(this.data.lnglat)
  },
  onShow() {
    
  },
  // 预报数据选择
  changeSelect(e) {
    let index = e.currentTarget.dataset.item
    this.setData({
      typeIndex:index
    })
  },
  showChart() {

  },
  // 跳转到城市卡片页面
  showCityCard() {
    wx.redirectTo({
      url: '/pages/cityList/cityList'
    })
  },
  methods: {
  },
})

